/*
 * IFT615 - Intelligence artificielle
 * Travail pratique #1 - Connect5
 *
 * Auteur : Éric Beaudry
 *
 * Université de Sherbrooke
 * Département d'informatique
 * 
 * JoueurConfig.java
 *
 * Created on 6 janv. 2009, 16:23:22
 */

package connect5;

import connect5.ia.JoueurArtificiel;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Eric Beaudry
 */
public class JoueurConfig extends javax.swing.JPanel {

    /** Creates new form JoueurConfig */
    public JoueurConfig() {
        initComponents();
    }

    public Joueur getJoueur(){
        if(manuelRB.isSelected())
            return null;
        if(interneRB.isSelected())
            return new JoueurArtificiel();
        if(externRB.isSelected()){
            try{
                JoueurClientCmd j = new JoueurClientCmd(pathTF.getText());
                j.setDebug(debugCheckBox.isSelected());
                return j;
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
        if(remoteRB.isSelected()){
            try{
                JoueurClientSocket j = new JoueurClientSocket(hostTF.getText(), Integer.parseInt(portTF.getText()));
                //j.setDebug(debugCheckBox.isSelected());
                return j;
            }catch(Exception e){
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, e,
                        "Echec connexion à " + hostTF.getText() + ":" + portTF.getText(),
                        JOptionPane.ERROR_MESSAGE);
            }
        }
        return null;

    }

    static JFileChooser filechooser = new JFileChooser();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        buttonGroup1 = new javax.swing.ButtonGroup();
        manuelRB = new javax.swing.JRadioButton();
        interneRB = new javax.swing.JRadioButton();
        externRB = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        pathTF = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        remoteRB = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        hostTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        portTF = new javax.swing.JTextField();
        debugCheckBox = new javax.swing.JCheckBox();

        jButton1.setText("jButton1");

        jTextField1.setText("jTextField1");

        setMinimumSize(new java.awt.Dimension(439, 111));
        setLayout(new java.awt.GridBagLayout());

        buttonGroup1.add(manuelRB);
        manuelRB.setSelected(true);
        manuelRB.setText("Manuel (par clics de souris)  ");
        manuelRB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manuelRBActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(manuelRB, gridBagConstraints);

        buttonGroup1.add(interneRB);
        interneRB.setText("Artificiel interne (classe connect5.ia.JoueurArtificiel)    ");
        interneRB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                interneRBActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(interneRB, gridBagConstraints);

        buttonGroup1.add(externRB);
        externRB.setText("Artificiel externe (interface stdin / stdout - classe connect5.JoueurClientCmd)   ");
        externRB.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                externRBStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(externRB, gridBagConstraints);

        jLabel1.setText("Commande: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        add(jLabel1, gridBagConstraints);

        pathTF.setText("/home/user/ift615/a.out");
        pathTF.setEnabled(false);
        pathTF.setMinimumSize(new java.awt.Dimension(250, 22));
        pathTF.setPreferredSize(new java.awt.Dimension(240, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 100.0;
        add(pathTF, gridBagConstraints);

        browseButton.setText("...");
        browseButton.setEnabled(false);
        browseButton.setMinimumSize(new java.awt.Dimension(35, 29));
        browseButton.setPreferredSize(null);
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(browseButton, gridBagConstraints);

        buttonGroup1.add(remoteRB);
        remoteRB.setText("Joueur distant (interface socket - classe connect5.JoueurClientSocket)  ");
        remoteRB.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                remoteRBStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(remoteRB, gridBagConstraints);

        jLabel2.setText("Hôte :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        add(jLabel2, gridBagConstraints);

        hostTF.setText("planiart.usherbrooke.ca");
        hostTF.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 100.0;
        add(hostTF, gridBagConstraints);

        jLabel3.setText("Port :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        add(jLabel3, gridBagConstraints);

        portTF.setText("1199");
        portTF.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 3.0;
        add(portTF, gridBagConstraints);

        debugCheckBox.setText("Mode déboguage  (affiche des traces à la console)  ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(debugCheckBox, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void manuelRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manuelRBActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_manuelRBActionPerformed

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        if(filechooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
            pathTF.setText(filechooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_browseButtonActionPerformed

    private void externRBStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_externRBStateChanged
        pathTF.setEnabled(externRB.isSelected());
        browseButton.setEnabled(externRB.isSelected());
    }//GEN-LAST:event_externRBStateChanged

    private void remoteRBStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_remoteRBStateChanged
        hostTF.setEnabled(remoteRB.isSelected());
        portTF.setEnabled(remoteRB.isSelected());
    }//GEN-LAST:event_remoteRBStateChanged

    private void interneRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_interneRBActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_interneRBActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox debugCheckBox;
    public javax.swing.JRadioButton externRB;
    private javax.swing.JTextField hostTF;
    public javax.swing.JRadioButton interneRB;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    public javax.swing.JRadioButton manuelRB;
    public javax.swing.JTextField pathTF;
    private javax.swing.JTextField portTF;
    public javax.swing.JRadioButton remoteRB;
    // End of variables declaration//GEN-END:variables

}
